package org.jsrml.core.purchaser.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.jsrml.common.persistence.StringIdBaseEntity;
import org.jsrml.common.util.UUIDGenerator;
import org.jsrml.core.base.entity.BaseStatus;
import org.jsrml.core.purchaser.command.SavePositionsCo;

/**
 * @author 张剑
 */
@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "POSITIONS")
@SuppressWarnings("serial")
public class Positions extends StringIdBaseEntity {
	
	@Column(name = "POSITION_CODE", length = 64)
	private String positionCode;
	
	@Column(name = "POSITION_NAME", length = 64)
	private String positionName;

	@Column(name = "DESCRIPT", columnDefinition = M.TEXT_COLUMN)
	private String descript;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "DEPARTMENT_ID",foreignKey = @ForeignKey(name = "none"))
	private Department department;

	private BaseStatus status;
	
	public void create(SavePositionsCo command,Department department){
		setId(UUIDGenerator.getUUID());
		setPositionCode(command.getPositionCode());
		setPositionName(command.getPositionName());
		setDescript(command.getDescript());
		setDepartment(department);
		
		setStatus(new BaseStatus());
		getStatus().setRemove(false);
		getStatus().setCreateDate(new Date());
	}
	
	public void modify(SavePositionsCo command,Department department){
		setPositionCode(command.getPositionCode());
		setPositionName(command.getPositionName());
		setDescript(command.getDescript());
		setDepartment(department);
		
		if(getStatus()==null){
			setStatus(new BaseStatus());
		}
		getStatus().setUpdateDate(new Date());
	}
	
	public void remove() {
		getStatus().setRemove(true);
	}
	
	public String getPositionCode() {
		return positionCode;
	}

	public void setPositionCode(String positionCode) {
		this.positionCode = positionCode;
	}

	public String getPositionName() {
		return positionName;
	}

	public void setPositionName(String positionName) {
		this.positionName = positionName;
	}

	public String getDescript() {
		return descript;
	}

	public void setDescript(String descript) {
		this.descript = descript;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public BaseStatus getStatus() {
		return status;
	}

	public void setStatus(BaseStatus status) {
		this.status = status;
	}

}
